package com.ds.controller;

import com.ds.mapper.UserCdkMapper;
import com.ds.mapper.UserMapper;
import com.ds.pojo.User;
import com.ds.service.cdkService.UserCdkService;
import com.ds.service.ceraService.UserCeraService;
import com.ds.service.userService.UserService;
import com.ds.vo.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.rmi.server.UID;
import java.util.List;
import java.util.Map;


/**
 * @author wzy
 * @version 1.0
 * @date 2022/5/23 21:06
 */

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserService userService;

    @Autowired
    private UserCeraService userCeraService;

    @Autowired
    private UserCdkService userCdkService;


    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){

        if (StringUtils.isEmpty(user.getAccountname()) || StringUtils.isEmpty(user.getPassword())){
            return new AjaxResult(-999,"账号或密码不能为空", "");
        }
        String token = userService.findUser(user);
        if (StringUtils.isEmpty(token)){
            return new AjaxResult(-999, "用户不存在", "");
        }

        return new AjaxResult(token);
    }


    /**
     * 余额查询
     * @param accountname
     * @return
     */
    @PostMapping("/getUserCera")
    public AjaxResult getUserCera(String accountname){
        if(StringUtils.isEmpty(accountname)){
            return new AjaxResult(-999, "用户名不能为空", "");
        }

        //获取用户uid
        String uid = userService.getUidByName(accountname);
        //根据用户uid查询余额
        String userCera = userCeraService.getUserCera(uid);
        return new AjaxResult(userCera);
    }


    /**
     * 用户点券余额扣减
     * @param param
     * @return
     */
    @PostMapping("/updateUserCera")
    public AjaxResult updateUserCera(@RequestBody Map param){
        logger.info("updateUserCera接口 param = {}",param);
        String accountName = (String) param.get("accountname");
        String cera = param.get("cera").toString();

        //获取用户uid
        String uid = userService.getUidByName(accountName);

        //更新用户余额
        Integer count = userCeraService.updateUserCera(uid, cera);
        if (count != 0){
            logger.info("余额更新成功");
            return new AjaxResult("余额更新成功","");
        }
        return new AjaxResult("余额更新失败","");
    }

    /**
     * 查询用户所持有的兑换码
     * @param accountname
     * @return
     */
    @GetMapping("/getUserCdkInfo")
    public AjaxResult getUserCdkInfo(String accountname){
        if (StringUtils.isEmpty(accountname)){
            return new AjaxResult(-999, "用户名不能为空", "");
        }
        //获取用户uid
        String uid = userService.getUidByName(accountname);

        //查询用户的兑换码
        List<Map> userCdk = userCdkService.getUserCdk(uid);
        return new AjaxResult(userCdk);
    }

    /**
     * 将用户抽到的道具兑换码入库
     * @param param
     * @return
     */
    @PostMapping("/insertUserCdk")
    public AjaxResult insertUserCdk(@RequestBody Map param){
        logger.info("insertUserCdk接口 param = {}",param);
        String accountName = (String) param.get("accountname");
        String cdk = (String) param.get("cdk");
        String cTitle = (String) param.get("cTitle");

        //获取用户uid
        String reward = userService.getUidByName(accountName);

        Integer count = userCdkService.insertUserCdk(cdk,cTitle,reward);
        if (count != 0){
            return new AjaxResult("兑换码存入成功","");
        }
        return new AjaxResult("兑换码存入失败","");
    }

}
